package com.bestcem.xm.ticket.controller.vo.ticket.config;

import com.bestcem.xm.ticket.entity.mongo.TicketTemplate;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 处理模板显示VO
 */
@Data
@JsonInclude
public class TicketConfigTemplateVO {
    @JsonProperty(value = "id")
    private String id;

    /**
     * orgId
     */
    @JsonProperty(value = "orgId")
    private String orgId;

    /**
     * ticketConfigId
     */
    @JsonProperty(value = "ticketConfigId")
    private String ticketConfigId;

    /**
     * category
     */
    @JsonProperty(value = "category")
    private Integer category;

    /**
     * customFieldLimit
     */
    @JsonProperty(value = "customFieldLimit")
    private Integer customFieldLimit;

    /**
     * defaultFields（字段已不再使用）
     */
    @JsonProperty(value = "defaultFields")
    private List<FieldInfo> defaultFields;

    /**
     * customFields
     */
    @JsonProperty(value = "customFields")
    private List<FieldInfo> customFields;


    @Data
    @JsonInclude
    public static class FieldInfo {

        /**
         * field
         */
        @JsonProperty(value = "field")
        private String field;

        /**
         * text
         */
        @JsonProperty(value = "text")
        private String text;

        /**
         * type
         */
        @JsonProperty(value = "type")
        private String type;

        /**
         * required
         */
        @JsonProperty(value = "required")
        private Object required;

        /**
         * options
         */
        @JsonProperty(value = "options")
        private List<Option> options;


        @Data
        @JsonInclude
        public static class Option {

            /**
             * text
             */
            @JsonProperty(value = "text")
            private String text;

            /**
             * value
             */
            @JsonProperty(value = "value")
            private Integer value;

            @Nullable
            public static Option getInstance(TicketTemplate.FieldInfo.Option entity) {
                if (Objects.isNull(entity)) {
                    return null;
                }
                Option data = new Option();
                data.setText(entity.getText());
                data.setValue(entity.getValue());
                return data;
            }
        }

        @Nullable
        public static FieldInfo getInstance(TicketTemplate.FieldInfo entity) {
            if (Objects.isNull(entity)) {
                return null;
            }
            FieldInfo data = new FieldInfo();
            data.setField(entity.getField());
            data.setText(entity.getText());
            data.setType(entity.getType());
            data.setRequired(entity.getRequired());

            List<Option> optionList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(entity.getOptions())) {
                List<TicketTemplate.FieldInfo.Option> options = entity.getOptions();
                for (TicketTemplate.FieldInfo.Option option : options) {
                    optionList.add(Option.getInstance(option));
                }
            }
            data.setOptions(optionList);

            return data;
        }
    }


    @JsonProperty(value = "record_flag")
    private Integer recordFlag = 1;

    @JsonProperty("created_id")
    private String createdId;

    @JsonProperty("created_dt")
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
    private Date createdDate;

    @JsonProperty("updated_id")
    private String updatedId;

    @JsonProperty("updated_dt")
    @JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss", timezone = "GMT+8")
    private Date updatedDate;
}
